home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / ICON_8 / ICONX_FO / FSCAN.C < prev    next >
Text File  |  1990-03-02  |  3KB  |  162 lines

  1. /*
  2.  * File: fscan.c
  3.  *  Contents: move, pos, tab.
  4.  */
  5.  
  6. #include "::h:config.h"
  7. #include "::h:rt.h"
  8. #include "rproto.h"
  9.  
  10. #ifdef PreProcess
  11. /* include(../M4/fncs.m4) /* */
  12. /* */
  13. #endif                    /* PreProcess */
  14.  
  15. /*
  16.  * move(i) - move &pos by i, return substring of &subject spanned.
  17.  *  Reverses effects if resumed.
  18.  */
  19. FncDcl(move,1)
  20.    {
  21.    register word i, j;
  22.    word oldpos;
  23.  
  24.    /*
  25.     * Arg1 must be a (non-long) integer.
  26.     */
  27.    switch (cvint(&Arg1)) {
  28.  
  29.       case T_Integer:
  30.          j = (word)IntVal(Arg1);
  31.          break;
  32.  
  33.       default:
  34.          RunErr(101, &Arg1);
  35.       }
  36.  
  37.    /*
  38.     * Save old &pos.  Local variable i holds &pos before the move.
  39.     */
  40.    oldpos = i = k_pos;
  41.  
  42.    /*
  43.     * If attempted move is past either end of the string, fail.
  44.     */
  45.    if (i + j <= 0 || i + j > StrLen(k_subject) + 1)
  46.       Fail;
  47.  
  48.    /*
  49.     * Set new &pos.
  50.     */
  51.    k_pos += j;
  52.  
  53.    /*
  54.     * Make sure j >= 0.
  55.     */
  56.    if (j < 0) {
  57.       i += j;
  58.       j = -j;
  59.       }
  60.  
  61.    /*
  62.     * Suspend substring of &subject that was moved over.
  63.     */
  64.    StrLen(Arg0) = j;
  65.    StrLoc(Arg0) = StrLoc(k_subject) + i - 1;
  66.    Suspend;
  67.  
  68.    /*
  69.     * If move is resumed, restore the old position and fail.
  70.     */
  71.    if (oldpos > StrLen(k_subject) + 1) {
  72.       RunErr(205, &tvky_pos.kyval)
  73.       }
  74.    else
  75.       k_pos = oldpos;
  76.    Fail;
  77.    }
  78.  
  79. /*
  80.  * pos(i) - test if &pos is at position i in &subject.
  81.  */
  82. FncDcl(pos,1)
  83.    {
  84.    register word i;
  85.  
  86.    /*
  87.     * Arg1 must be an integer.
  88.     */
  89.    if (cvint(&Arg1) == CvtFail) 
  90.       RunErr(101, &Arg1);
  91.  
  92.    /*
  93.     * Fail if &pos is not equivalent to Arg1, return Arg1 otherwise.
  94.     */
  95.    if ((i = cvpos(IntVal(Arg1), StrLen(k_subject))) != k_pos)
  96.       Fail;
  97.    MakeInt(i, &Arg0);
  98.    Return;
  99.    }
  100.  
  101. /*
  102.  * tab(i) - set &pos to i, return substring of &subject spanned.
  103.  *  Reverses effects if resumed..
  104.  */
  105.  
  106. FncDcl(tab,1)
  107.    {
  108.    register word i, j;
  109.    word t, oldpos;
  110.  
  111.    /*
  112.     * Arg1 must be an integer.
  113.     */
  114.    if (cvint(&Arg1) == CvtFail) 
  115.       RunErr(101, &Arg1);
  116.  
  117.    /*
  118.     * Convert it to an absolute position.
  119.     */
  120.    j = cvpos(IntVal(Arg1), StrLen(k_subject));
  121.    if (j == CvtFail)
  122.       Fail;
  123.  
  124.    /*
  125.     * Save old &pos.  Local variable i holds &pos before the tab.
  126.     */
  127.    oldpos = i = k_pos;
  128.  
  129.    /*
  130.     * Set new &pos.
  131.     */
  132.    k_pos = j;
  133.  
  134.    /*
  135.     *  Make j the length of the substring &subject[i:j]
  136.     */
  137.    if (i > j) {
  138.       t = i;
  139.       i = j;
  140.       j = t - j;
  141.       }
  142.    else
  143.       j = j - i;
  144.  
  145.    /*
  146.     * Suspend the portion of &subject that was tabbed over.
  147.     */
  148.    StrLoc(Arg0) = StrLoc(k_subject) + i - 1;
  149.    StrLen(Arg0) = j;
  150.    Suspend;
  151.  
  152.    /*
  153.     * If tab is resumed, restore the old position and fail.
  154.     */
  155.    if (oldpos > StrLen(k_subject) + 1) {
  156.       RunErr(205, &tvky_pos.kyval);
  157.       }
  158.    else
  159.       k_pos = oldpos;
  160.    Fail;
  161.    }
  162.